<?php
/**
 * 网站api统一入口, 适用php5.6^|php7^ 版本v1.5.3
 *
 * cighsen02 365182575
 * time: 2022.01.15
 * update: 2024.05.01
**/

/**
 * 开启dubug
 * 此方便方式，在线临时调试用，生产环境 define('DEBUG', false)
 */
define('DEBUG', isset($_GET['debug']) || isset($_POST['debug']));

require dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'e' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'connect.php';

/**
 * $action 动作有： info, user等
 *
 * 操作类型,支持POST/GET
 */
$action = isset($_POST['act']) ? $_POST['act'] : (isset($_GET['act']) ? $_GET['act'] : null);

if (in_array($action, ['logout', 'login', 'register', 'set_fava', 'show_key'])) {

    // 方法
    $method = false !== strstr($action, '_') ? camel_case($action) : $action;
    $action = 'user';
}
// 调用信息时简化参数 不传act参数
else if (is_null($action) && (isset($_GET['classid']) || isset($_GET['tablename']))) {

    $action = 'info';
}

/**
 * api工作方式：html、json 默认为api方式
*/
define('API_WORK', !in_array($action, ['generate', 'qrcode']));

//来源，可以设置相应的请求头
if (array_key_exists('HTTP_ORIGIN', $_SERVER)) {

	$origin = isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN'] ? $_SERVER['HTTP_ORIGIN'] : null;
}
elseif (array_key_exists('HTTP_REFERER', $_SERVER)) {

	$origin = isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : null;
}
else {

	$origin = isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] ? $_SERVER['REMOTE_ADDR'] : null;
}

/**
 * 允许请求GET POST OPTIONS请求方法，其它不允许
 *
 * 特殊需要可以自己添加
 */
if(!in_array($_SERVER['REQUEST_METHOD'], ['GET', 'POST', 'OPTIONS']) || is_null($action)) throw new Exception("非法请求", 9401);

// 开启调试模式
if(defined('DEBUG') && DEBUG) {

	//显示所有错误信息
	ini_set("display_errors", "On");
	error_reporting(E_ALL);
}

//判断是否后台管理员 true为后台管理 引用帝国文件
if ($origin && 0 === strpos($origin, '/ecmseditor')) {

    require ECMS_PATH . DIRECTORY_SEPARATOR . 'e' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'functions.php';
    require ECMS_PATH . 'e' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 't_functions.php';
}
else{

    require ECMS_PATH . 'e' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'q_functions.php';
    require ECMS_PATH . 'e' . DIRECTORY_SEPARATOR . 'member' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'user.php';
    require ECMS_PATH . 'e' . DIRECTORY_SEPARATOR . 'member' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'member_registerfun.php';
}
require ECMS_PATH . 'e' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'dbcache' . DIRECTORY_SEPARATOR . 'class.php';
require ECMS_PATH . 'e' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'dbcache' . DIRECTORY_SEPARATOR . 'MemberLevel.php';

$link	= db_connect();
$empire	= new mysqlquery();

switch($action) {

	// 获取信息
	case 'info':

	    $data = forward_static_call_array([Twoir\Controller\Info::class, 'get'], [$_GET]);
	    break;

	// 会员
	case 'user':

	    $data = forward_static_call_array([Twoir\Controller\Member::class, $method], [in_array($method, ['logout', 'setFava', 'showKey'], true) ? $_GET : $_POST]);
	    break;

	//非法请求
	default:

        throw new Exception("非法请求", 9401);
}

db_close();
$empire = null;

//json格式输出
if (defined('API_WORK') && API_WORK) {

	//待返回的数据
	$result = [
	    'code'      => 0,
	    'message'   => 'Ok'
	];

	if (isset($data) && $data) $result['data'] = $data;

	$data = json_encode($result, 15);

	header("Content-type:application/json");
}

//定义标准输出
else {

	header('content-type:text/html;charset=utf-8');
}

echo $data;
